package com.jilian.mydemo.modul.shop.ui;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseEventMsg;
import com.jilian.mydemo.base.BaseFragment;
import com.jilian.mydemo.event.RxBus;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.listener.ViewPagerItemClickListener;
import com.jilian.mydemo.modul.main.adapter.BananerPagerAdapter;
import com.jilian.mydemo.modul.main.adapter.GoodBananerPagerAdapter;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.views.SlideSeeMoreLayout;
import com.jilian.mydemo.views.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class GoodsDetailLeftFragment extends BaseFragment implements CustomItemClickListener ,ViewPagerItemClickListener {

    private ViewPager viewPager;
    private LinearLayout llIndcator;
    private List<String> list;
    private GoodBananerPagerAdapter bananerPagerAdapter;
    private SlideSeeMoreLayout layout;

    private TextView tvName;
    private TextView tvPrice;
    private ImageView ivDel;
    private TextView tvCount;
    private ImageView ivAdd;
    private TextView tvGoodCount;
    private TextView tvConent;
    private UserViewModel viewModel;
    private WebView webview;
    private Context mCtx;



    public int getCount() {
        return Integer.parseInt(tvCount.getText().toString());
    }


    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_goodsdetailleft;
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        llIndcator = (LinearLayout) view.findViewById(R.id.ll_indcator);
        layout = (SlideSeeMoreLayout) view.findViewById(R.id.layout);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvPrice = (TextView) view.findViewById(R.id.tv_price);
        ivDel = (ImageView) view.findViewById(R.id.iv_del);
        tvCount = (TextView)view. findViewById(R.id.tv_count);
        ivAdd = (ImageView) view.findViewById(R.id.iv_add);
        tvGoodCount = (TextView) view.findViewById(R.id.tv_good_count);
        tvConent = (TextView) view.findViewById(R.id.tv_conent);
        list = new ArrayList<>();
        webview = (WebView) view.findViewById(R.id.webview);

    }
    /**
     * 初始化banner
     */
    private void initBanner() {
        viewPager.addOnPageChangeListener(new ViewPagerIndicator(getmActivity(), viewPager, llIndcator, list.size()));
        bananerPagerAdapter = new GoodBananerPagerAdapter(getmActivity(),list,this);
        viewPager.setAdapter(bananerPagerAdapter);
    }
    @Override
    protected void initData() {

    }

    /**
     * 加载html标签
     *
     * @param bodyHTML
     * @return
     */
    private String getHtmlData(String bodyHTML) {
        String head = "<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                "<style>img{max-width: 100%; width:auto; height:auto!important;}</style>" +
                "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }


    @Override
    protected void initListener() {
        ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer count = Integer.parseInt(tvCount.getText().toString());
                if(count==1){
                    return;
                }
                count--;
                tvCount.setText(String.valueOf(count));
            }
        });
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer count = Integer.parseInt(tvCount.getText().toString());
                count++;
                tvCount.setText(String.valueOf(count));
            }
        });
        RxBus.getInstance().toObservable().map(new Function<Object, BaseEventMsg>() {
            @Override
            public BaseEventMsg apply(Object o) throws Exception {
                return (BaseEventMsg) o;
            }
        }).subscribe(new Consumer<BaseEventMsg>() {
            @Override
            public void accept(BaseEventMsg dto) throws Exception {
                if (dto != null&&dto.getProductDetailDto()!=null) {
                    tvName.setText(dto.getProductDetailDto().getProductName());
                    tvPrice.setText("￥"+dto.getProductDetailDto().getPrice());
                    tvGoodCount.setText("（库存"+dto.getProductDetailDto().getQuantity()+"）");
                    tvConent.setText(dto.getProductDetailDto().getProductIntroduction());
                    //content是后台返回的h5标签
                    webview.loadDataWithBaseURL(null,
                            getHtmlData(dto.getProductDetailDto().getProductDetail()), "text/html", "utf-8", null);
                    //
                    if(EmptyUtils.isNotEmpty(dto.getProductDetailDto().getProductImageList())){
                        list.clear();
                        list.addAll(dto.getProductDetailDto().getProductImageList());
                        initBanner();
                    }
                }
            }
        });
        layout.setOnSlideDetailsListener(new SlideSeeMoreLayout.OnSlideDetailsListener() {
            @Override
            public void onStateChanged(SlideSeeMoreLayout.Status status) {
                if (status == SlideSeeMoreLayout.Status.OPEN) {
                    //当前为查看更多页
                    ((GoodDetailActivity) getActivity()).rlCenter.setVisibility(View.VISIBLE);
                    ((GoodDetailActivity) getActivity()).tvCenter.setTextColor(getResources().getColor(R.color.color_main_title));


                    ((GoodDetailActivity) getActivity()).rlLeft.setVisibility(View.INVISIBLE);
                    ((GoodDetailActivity) getActivity()).tvLeft.setTextColor(getResources().getColor(R.color.color_hint));



                } else {
                    //当前为商品页
                    ((GoodDetailActivity) getActivity()).rlLeft.setVisibility(View.VISIBLE);
                    ((GoodDetailActivity) getActivity()).tvLeft.setTextColor(getResources().getColor(R.color.color_main_title));

                    ((GoodDetailActivity) getActivity()).rlCenter.setVisibility(View.INVISIBLE);
                    ((GoodDetailActivity) getActivity()).tvCenter.setTextColor(getResources().getColor(R.color.color_hint));


                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onItemClick(View view, int position) {

    }
    @Override
    public void onViewPageItemClick(View view, int position) {

    }

}
